# -*- coding: utf-8 -*-

'''eof
name:申请人当前是否缴纳住房公积金
code:HPF_PayStatus
tableName:
columnName:
groups:个人通用
dependencies:IC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import datetime
import re

reload(sys)
sys.setdefaultencoding('utf-8')

'''
申请人当前是否缴纳住房公积金 
    提取个人征信报告-住房公积金参缴记录中的“月缴存额”和“缴费状态”字段进行判断
'''
null_type_list = ['', None, 'null', 'NULL', '/', 'None', 'Null',  ' ']

def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
        :return:该表全部数据  返回[{}]为异常缺失，返回0为正常缺失，其他返回正常数组
        '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or len(table) == 0 or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0 \
                    or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]

def ConvertDataType(dataValue, dataType):
    returnData = None
    if dataType == 'float':
        try:
            returnData = float(dataValue) if float(dataValue) >= 0 else None
        except:
            returnData = None
    elif dataType == 'None':
        returnData = 0 if dataValue is None else dataValue
    return returnData

def isfloat(x):
    '''
    判断x为float类型且大于等于0
    '''
    try:
        if isinstance(float(x), float) and float(x) >= 0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def HPF_PayStatus_2D():
    try:
        data = IC_DSJ_2D.get("data")
        PHF = getTable(data, "PHF", 1, 0)
        if PHF == [{}]:
            return u'缺失值'
        elif PHF == 0:
            return u'否'
        else:
            result = []
            halfYearsAgo = (datetime.datetime.now().year) * 12 + datetime.datetime.now().month - 6
            monthToday = (datetime.datetime.now().year) * 12 + datetime.datetime.now().month
            PF05 = getTable(PHF[0], "PF05", 1, 0)
            if PF05 == [{}]:
                result.append(None)
            elif PF05 == 0:
                result.append(0)
            else:
                for i in PF05:
                    PF05A = getTable(i, "PF05A", 1, 1)
                    if PF05A == [{}]:
                        result.append(None)
                    elif PF05A == 0:
                        result.append(0)
                    else:
                        PF05AD01 = PF05A[0].get("PF05AD01")
                        PF05AJ01 = PF05A[0].get("PF05AJ01")
                        PF05AR03 = PF05A[0].get("PF05AR03") # 缴至月份

                        ToMonth = re.compile(r'\D+').sub('', PF05AR03)
                        ToMonthFact = float(ToMonth[:4]) * 12 + float(ToMonth[4:6]) if float(ToMonth[4:6]) <= 12 else float(
                            ToMonth[4:5])
                        if ToMonthFact <= monthToday and ToMonthFact > halfYearsAgo:
                            if PF05AD01 not in null_type_list and PF05AJ01 not in null_type_list:
                                if isfloat(PF05AJ01) and float(PF05AJ01) > 0 and PF05AD01 == u"1":
                                    result.append(1)
                                else:
                                    result.append(0)
                            else:
                                result.append(None)

            returnListNotNull = filter(lambda x: x is not None, result)
            if len(returnListNotNull) == 0:
                return u'缺失值'
            elif sum(returnListNotNull) > 0:
                return u'是'
            elif sum(returnListNotNull) == 0:
                return u'否'
    except:
        return u'缺失值'
result = HPF_PayStatus_2D()

